// vite.config.js
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import ReactivityTransform from "@vue-macros/reactivity-transform/vite";
import path from "path";

export default defineConfig(({ mode, command }) => {
  return {
    plugins: [
      vue(),
      ReactivityTransform(),
      AutoImport({
        imports: ["vue", "vue-router"],
        dts: path.resolve(__dirname, "src/auto-imports.d.ts"),
      }),
    ],
    resolve: {
      alias: {
        "~": path.resolve(__dirname, "./"),
        "@": path.resolve(__dirname, "./src"),
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    css: {
      modules: true,
      preprocessorOptions: {
        less: {
          modifyVars: {
            "@primary-color": "#1890ff",
            "@font-size-base": "14px",
          },
          javascriptEnabled: true,
        },
      },
    },
    server: {
      host: "0.0.0.0",
      port: 3000,
      proxy: {
        // 代理 /api 路径的请求到 http://27.58.248.48:8082
        "/api": {
          target: "http://172.20.19.163:88/",
          changeOrigin: true,
          ws: true,
          ChangeOrigin: true,
          // rewrite: (path) => path.replace(/^\/api/, ""),
        },
        // 代理 /jfzg 路径的请求到 http://172.20.19.160:81
        "/jfzg": {
          target: "http://172.20.19.160:81",
          changeOrigin: true,
          ws: true,
          // rewrite: (path) => path.replace(/^\/jfzg/, "jfzg"),
        },
        // 如果有更多 API，可以继续添加更多的代理规则
      },
    },
    build: {
      outDir: "dist",
      assetsDir: "assets",
      sourcemap: true,
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
          passes: 3,
        },
        mangle: {
          toplevel: true,
          properties: {
            regex: /^_/,
          },
        },
      },
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        input: {
          main: path.resolve(__dirname, "index.html"),
        },
        output: {
          // 让 Vite 根据模块依赖自动进行代码分割
        },
      },
    },
  };
});
